﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AirportCreatorTool {
    public partial class AirportCreator : Form {
        private AirportLayout airportLayout;
        private RunwayLayout newRunway;
        private TaxiwayLayout newTaxiway;

        public AirportCreator() {
            InitializeComponent();

            airportLayout = new AirportLayout();
        }

        private void AirportCreator_Load(object sender, EventArgs e) {
            defaultPanel.Visible = true;
            addRunwayPanel.Visible = false;
            addTaxiwayPanel.Visible = false;
        }

        private void runwayImage_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            newRunway.AddPoint(new Point(e.X, e.Y));
            newRunway.Draw(runwayImage.CreateGraphics());
        }

        private void addRunwayButton_Click(object sender, EventArgs e) {
            toggleRunwayPanel();

            newRunway = new RunwayLayout();
        }

        private void saveRunwayButton_Click(object sender, EventArgs e) {
            if(this.direction1TextBox.Text != String.Empty &&
                this.direction2Textbox.Text != String.Empty &&
                this.maxWeightTextbox.Text != String.Empty &&
                this.widthTextbox.Text != String.Empty &&
                newRunway.Points.Count > 1) {
                newRunway.Direction1 = this.direction1TextBox.Text;
                newRunway.Direction2 = this.direction2Textbox.Text;
                newRunway.MaxWeight = Convert.ToInt32(this.maxWeightTextbox.Text);
                newRunway.Width = Convert.ToInt32(this.widthTextbox.Text);
                newRunway.Arrivals = this.runwayArrivalsCheckbox.Checked;
                newRunway.Departures = this.runwayDeparturesCheckbox.Checked;
                newRunway.Save();

                airportLayout.AddRunway(newRunway);

                endRunway();
            } else {
                MessageBox.Show("Please add runway nodes and fill out all fields before saving.");
            }
        }

        private void cancelRunwayButton_Click(object sender, EventArgs e) {
            endRunway();
        }

        private void taxiwayImage_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            newTaxiway.AddPoint(new Point(e.X, e.Y));
            newTaxiway.Draw(taxiwayImage.CreateGraphics());
        }

        private void addTaxiwayButton_Click(object sender, EventArgs e) {
            toggleTaxiwayPanel();

            newTaxiway = new TaxiwayLayout();
        }

        private void saveTaxiwayButton_Click(object sender, EventArgs e) {
            if(this.taxiwayNameTextbox.Text != String.Empty &&
                this.maxWeightTTextbox.Text != String.Empty &&
                this.widthTTextbox.Text != String.Empty &&
                newTaxiway.Points.Count > 1) {
                newTaxiway.Name = this.taxiwayNameTextbox.Text;
                newTaxiway.MaxWeight = Convert.ToInt32(this.maxWeightTTextbox.Text);
                newTaxiway.Width = Convert.ToInt32(this.widthTTextbox.Text);
                newTaxiway.Save();

                airportLayout.AddTaxiway(newTaxiway);

                endTaxiway();
            } else {
                MessageBox.Show("Please add taxiway nodes and fill out all fields before saving.");
            }
        }

        private void cancelTaxiwayButton_Click(object sender, EventArgs e) {
            endTaxiway();
        }

        private void endRunway() {
            this.direction1TextBox.Text = String.Empty;
            this.direction2Textbox.Text = String.Empty;
            this.maxWeightTextbox.Text = String.Empty;
            this.widthTextbox.Text = String.Empty;
            this.runwayArrivalsCheckbox.Checked = false;
            this.runwayDeparturesCheckbox.Checked = false;

            newRunway = null;

            toggleRunwayPanel();
        }

        private void endTaxiway() {
            this.taxiwayNameTextbox.Text = String.Empty;
            this.maxWeightTTextbox.Text = String.Empty;
            this.widthTTextbox.Text = String.Empty;

            newTaxiway = null;

            toggleTaxiwayPanel();
        }

        private void toggleRunwayPanel() {
            addRunwayPanel.Visible = !addRunwayPanel.Visible;
            defaultPanel.Visible = !defaultPanel.Visible;
        }

        private void saveLayoutButton_Click(object sender, EventArgs e) {
            airportLayout.SaveToFile();
        }

        private void toggleTaxiwayPanel() {
            addTaxiwayPanel.Visible = !addTaxiwayPanel.Visible;
            defaultPanel.Visible = !defaultPanel.Visible;
        }

        private void image_Paint(object sender, PaintEventArgs e) {
            airportLayout.DrawSurfaces(e.Graphics);
        }
    }
}
